var currentLangCode = 'zh-cn';
var myDate = new Date();
var dateNows= myDate.toLocaleDateString();
	$(function() {
		
		$('#calendarInput').datepicker({
			format:'yy-mm-dd',
			dateFormat:'yy-mm-dd',
			changeMonth: true,
			flat: true,
			changeYear: true,   // 允许选择年份
			date: dateNows,
			current: dateNows,
			position: 'r', 
			calendars: 1,
			starts: 1,
			onSelect: function(formated, datesyou){
				customerDate=formated;
						$('#calendar').fullCalendar('destroy');
						renderCalendar(customerDate);
			}
		});
		$("#locale").change(function() {
			$('#calendarInput').datepicker('option', $.extend({showMonthAfterYear: true},
				$.datepicker.regional[$(this).val()]));
		});
		
		$(".datepicker").datepicker({minDate: -3,maxDate: 3});
		/*
			$("#startdate").datepicker(
		{onSelect: function(dateText, inst) {$('#enddate').datepicker('option', 'minDate',new Date(dateText.replace('-',',')));}
		});
		*/
	});

	function renderCalendar(customerDate) {
		var riliDate ='2014-06-12';
		if(customerDate !=""){
			riliDate =customerDate;
		}
		$('#calendar').fullCalendar({
			header: {
				left: 'prev,next today',
				center: 'title',
				right: ''
			},
			defaultDate: riliDate,
			lang: currentLangCode,
			buttonIcons: false, // show the prev/next text
			weekNumbers: false,
			draggable:true,
			events:fangArray,
			dayClick: function(date, allDay, jsEvent, view) {
				alert(55);
	    	},
			eventClick: function(calEvent, jsEvent, view) {
				alert(59);
				//window.location.href='order_add.html';
	    	}
	    	
		});
	}
	
	function addeventf(){
		var addeventDiv =document.getElementById("addeventf");
		 addenentDiv.style.display=='block';
	}
	
	$(function(){

	$("#isallday").click(function(){
		if($("#sel_start").css("display")=="none"){
			$("#sel_start,#sel_end").show();
		}else{
			$("#sel_start,#sel_end").hide();
		}
	});
	
	$("#isend").click(function(){
		if($("#p_endtime").css("display")=="none"){
			$("#p_endtime").show();
		}else{
			$("#p_endtime").hide();
		}
		$.fancybox.resize();//调整高度自适应
	});
	
	//新建  日期操作
		$("#isendAdd").click(function(){
		if($("#p_endtimeAdd").css("display")=="none"){
			$("#p_endtimeAdd").show();
		}else{
			$("#p_endtimeAdd").hide();
		}
		if($("#sel_startAdd").css("display")=="inline"){
			$("#sel_startAdd,#sel_endAdd").show();
		}else{
			$("#sel_startAdd,#sel_endAdd").hide();
		}
		$.fancybox.resize();//调整高度自适应
	});
	
		$("#isalldayAdd").click(function(){
		if($("#sel_startAdd").css("display")=="none"){
			$("#sel_startAdd,#sel_endAdd").show();
		}else{
			$("#sel_startAdd,#sel_endAdd").hide();
		}
	});
	
	/*
	//提交表单
	$('#add_form').ajaxForm({
		beforeSubmit: showRequest, //表单验证
        success: showResponse //成功返回
    }); 
	*/
	//删除事件
	$("#del_event").click(function(){
		if(confirm("您确定要删除吗？")){
			var eventid = $("#eventid").val();
			$.post("url",{id:eventid},function(msg){
				if(msg==1){//删除成功
					$.fancybox.close();
					$('#calendar').fullCalendar('refetchEvents'); //重新获取所有事件数据
				}else{
					alert(msg);	
				}
			});
		}
	});
});

function showRequest(){
	var events = $("#event").val();
	if(events==''){
		alert("请输入日程内容！");
		$("#event").focus();
		return false;
	}
}

function showResponse(responseText, statusText, xhr, $form){
	if(statusText=="success"){	
		if(responseText==1){
			$.fancybox.close();
			$('#calendar').fullCalendar('refetchEvents'); //重新获取所有事件数据
		}else{
			alert(responseText);
		}
	}else{
		alert(statusText);
	}
}